package com.nexgo.payment.action.confirmpan;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nexgo.payment.R;
import com.nexgo.payment.action.Action;
import com.nexgo.payment.action.ActionResult;
import com.nexgo.payment.action.ActionStep;
import com.nexgo.payment.action.OnActionListener;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.entity.TransData;
import com.nexgo.payment.fragment.TextDialogFragment;
import com.nexgo.payment.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xiaox on 2017/2/14.
 */

public class ConfirmPanDialogFragment extends DialogFragment implements Action<ActionStep>, ConfirmPanContract.View {
    public static final String TRANS_DATA = "trans_data";
    @BindView(R.id.tv_card_no)
    TextView tvCardNo;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.ll_amount)
    LinearLayout llAmount;
    @BindView(R.id.confirm)
    Button confirm;
    @BindView(R.id.cancel)
    ImageView cancel;
    Unbinder unbinder;
    @BindView(R.id.ll_confirm_pan_background)
    LinearLayout llConfirmPanBackground;
    private OnActionListener mListener;
    private ConfirmPanContract.Presenter mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TransData transData = getArguments().getParcelable(TRANS_DATA);
        assert transData != null;
        new ConfirmPanPresenter(this, Repository.getInstance(), transData);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        llConfirmPanBackground.setVisibility(View.GONE);
    }

    public static ConfirmPanDialogFragment newInstance(@NonNull TransData transData) {
        ConfirmPanDialogFragment dialog = new ConfirmPanDialogFragment();
        Bundle b = new Bundle();
        b.putParcelable(TRANS_DATA, transData);
        dialog.setArguments(b);
        return dialog;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mPresenter != null) {
            mPresenter.start();
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnActionListener) {
            mListener = (OnActionListener) context;
        }
    }

    @Override
    public ActionStep getActionStep() {
        return ActionStep.CONFIRM_PAN;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_confirm_card_no, null);
        unbinder = ButterKnife.bind(this, view);
        getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCancelable(false);
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void setPresenter(ConfirmPanContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public void showConfirmPan(String pan, String amount) {
        llConfirmPanBackground.setVisibility(View.VISIBLE);
        llAmount.setVisibility(View.INVISIBLE);
        tvCardNo.setText(pan);
        if (!TextUtils.isEmpty(amount)) {
            llAmount.setVisibility(View.VISIBLE);
            tvAmount.setText(StringUtils.formatAmount(amount, SysConstant.FUND_DIGITS));
        }
        confirm.setOnClickListener(view1 -> confirm());
        cancel.setOnClickListener(view12 -> cancel());
    }

    @Override
    public void showOnBlacklist() {
        showErr(getString(R.string.on_blacklist));
    }

    protected void showErr(String tip) {
        llConfirmPanBackground.setVisibility(View.INVISIBLE);
        TextDialogFragment dialogFragment = TextDialogFragment.newInstance();
        dialogFragment.setDialogType(TextDialogFragment.DIALOG_TYPE_WRONG);
        dialogFragment.setTitleText(getString(R.string.tip));
        dialogFragment.setContentText(tip, Gravity.CENTER);
        dialogFragment.setPositionText(getString(R.string.confirm));
        dialogFragment.setDialogListener(new TextDialogFragment.OnDialogListener() {
            @Override
            public void onCancel() {

            }

            @Override
            public void onConfirm() {
                cancel();
            }
        });
        dialogFragment.show(getActivity().getSupportFragmentManager(), null);
    }

    @Override
    public void showPureEcCardBanForAuth() {
        showErr(getString(R.string.pure_ec_card_ban_for_auth));
    }

    private void confirm() {
        dismiss();
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(this, ActionResult.SUCC, true));
        }
    }

    private void cancel() {
        dismiss();
        if (mListener != null) {
            mListener.setActionResult(new ActionResult(this, ActionResult.ERR_CANCEL, false));
        }
    }

    @Override
    public void showConfirmed() {
        confirm();
    }

    @Override
    public boolean isActive() {
        return isAdded();
    }
}
